package com.jch.eduservice.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jch.commonutils.R;
import com.jch.eduservice.entity.EduTeacher;
import com.jch.eduservice.entity.vo.TeacherQuery;
import com.jch.eduservice.service.EduTeacherService;
import com.jch.servicebase.exceptionhandler.JchException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author jch
 * @since 2024-11-21
 */
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;


    /**
     * 查询所有数据
     */
    @ApiOperation(value = "查询所有讲师")
    @GetMapping("/findAll")
    public R getAllTeacher() {
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }

    /**
     * 逻辑删除讲师
     */
    @ApiOperation(value = "删除讲师")
    @DeleteMapping("/removeTeacher/{id}")
    public R removeTeacher(@ApiParam(name = "id", value = "讲师id", required = true) @PathVariable String id) {
        boolean flag = eduTeacherService.removeById(id);
        if(flag){
           return R.ok();
        }else {
            return R.error();
        }
    }
    /**
     * 讲师分页查询
     */
    @ApiOperation(value = "分页查询讲师")
    @GetMapping("/pageTeacher/{current}/{limit}")
    public R pageListTeacher(@ApiParam(name = "current", value = "当前页码", required = true) @PathVariable long current,
                             @ApiParam(name = "limit", value = "每页记录数", required = true)@PathVariable long limit){
        //创建分页对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //调用方法分页查询
        eduTeacherService.page(pageTeacher, null);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }
    /**
     * 条件查询带分页
     */
    @ApiOperation(value = "分页带条件查询讲师")
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public R getTeacherCondition(@ApiParam(name = "teacherQuery", value = "查询对象")@RequestBody (required = false) TeacherQuery teacherQuery,
                                 @ApiParam(name = "current", value = "当前页码", required = true) @PathVariable Long current,
                                 @ApiParam(name = "limit", value = "每页记录数", required = true ) @PathVariable Long limit){
        //创建分页对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //创建条件构造器对象
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //判断条件是否为空
        String name=teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_modified",end);
        }
        wrapper.orderByDesc("gmt_create");
        eduTeacherService.page(pageTeacher,wrapper);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }
    @ApiOperation(value = "添加讲师")
    @PostMapping("/addTeacher")
    public R addTeacher(@ApiParam(name = "teacher", value = "讲师对象", required = true) @RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if(save){
            return R.ok();
        }else {
            return R.error();
        }
    }
    @ApiOperation(value = "根据id查询讲师")
        @GetMapping("/getTeacher/{id}")
    public R getTeacher(@ApiParam(name = "id", value = "讲师id", required = true) @PathVariable String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }
    @ApiOperation(value = "修改讲师")
    @PostMapping("/updateTeacher")
    public R updateTeacher(@ApiParam(name = "teacher", value = "讲师对象", required = true) @RequestBody EduTeacher eduTeacher) {
        boolean b = eduTeacherService.updateById(eduTeacher);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

}

